<?php
/**
 * Application level View Helper
 *
 * This file is application-wide helper file. You can put all
 * application-wide helper-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.View.Helper
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Helper', 'View');
App::uses('FormHelper', 'Helper');

/**
 * Application helper
 *
 * Add your application-wide methods in the class below, your helpers
 * will inherit them.
 *
 * @package       app.View.Helper
 */
class AppHelper extends Helper {
	
	public $helpers = array('Form');
	
	private function _refactorOptions(&$options) {
		if(!empty($options)) {
			$tmp = array();
			foreach($options as $opt) {
				$tmp[$opt] = __($opt);
			}
			$options = $tmp;
		}
	}
	public function input($name, $type, $value="", $options=array()) {
		
		$pos = strstr($name, ".") ? strrpos($name, ".")+1 : 0;
		$justName = isset($options['label']) ? $options['label'] : substr($name, $pos);
		
		$labelValue = strtotitle(__(Multibyte::strtolower(__(Inflector::humanize($justName)))), true) . ':';
		$labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : array();
		$label = $this->Form->label($name, $labelValue, $labelOptions);
		$error = '<span class="error-message-container">' . $this->Form->error($name) . '</span>';
		switch($type) {
			case 'hidden':
				$label = "";
			case 'text':
				return $label . $this->Form->$type($name, array_merge($options, array('value' => $value))) . $error;
				
			case 'textarea':
			    return $label . $this->Form->textarea($name, array_merge($options, array('value'=>$value))) . $error;
			    
			case 'date':
				return $label . $this->Form->text($name, array('value' => $value, 'class'=>'datepicker', 'maxlength'=>10)) . $error;
				
			case 'select':
				if(empty($options['options_raw'])){
					$this->_refactorOptions($options["options"]);
				}
                $empty = isset($options['empty']) ? $options['empty'] : false;
				return $label . $this->Form->select($name, $options["options"], array('value' => $value, 'empty'=> $empty)) . $error;
				
			case 'radio':
				$this->_refactorOptions($options["options"]);
				$value = empty($value) ? array_shift(array_keys($options["options"])) : $value;
				return $this->Form->label('none', $labelValue) .
						'<div class="radio-container">' .
							$this->Form->radio($name, $options["options"] , array('value'=>$value, 'legend'=>false)) .
						'</div>';
				
			case 'autocomplete':
			    return $label .
			           '<span class="autocomplete">' .
			           $this->Form->hidden($name, array('value' => $value)).
			           $this->Form->text($name . '_keyword', array_merge($options, array(
			               'value'  => $options['autocomplete_value'],
			               'ac:url' => $options['url'])
			           )) .
			           '</span>';
				
			default:
				return $label;
		}
	}
}
